package com.tadpole.music.view.activity.login;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tadpole.music.R;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.iView.login.SendCodeIView;
import com.tadpole.music.presenter.login.FindPwdPresenter;
import com.tadpole.music.presenter.login.SendCodePresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.me.SettingActivity;

/**
 * 找回密码
 * Created By 孙岩
 * on 2020/9/10
 */
public class RetrievePwdActivity extends BaseActivity implements SendCodeIView, RegisterIView {
    //回退
    private View view_back_icon;
    //手机号
    private EditText etPhone;
    //验证码
    private EditText etCode;
    //发送验证码
    private TextView tvSendCode;
    //密码
    private EditText etPassword;
    //确定
    private TextView tvSubmit;
    //发送验证码
    private SendCodePresenter sendCodePresenter;
    //是否发送了验证码
    private boolean isSendCode = false;
    //倒计时
    private TimeCount time;
    //找回密码
    private FindPwdPresenter findPwdPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_pwd);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        time = new TimeCount(60000, 1000);
        initViews();
        initListeners();

        sendCodePresenter = new SendCodePresenter();
        sendCodePresenter.attachView(this);

        findPwdPresenter = new FindPwdPresenter();
        findPwdPresenter.attachView(this);
    }

    //初始化控件
    private void initViews() {
        view_back_icon = findViewById(R.id.view_back_icon);
        etPhone = findViewById(R.id.etPhone);
        etCode = findViewById(R.id.etCode);
        tvSendCode = findViewById(R.id.tvSendCode);
        etPassword = findViewById(R.id.etPassword);
        tvSubmit = findViewById(R.id.tvSubmit);
    }

    //初始化监听器
    private void initListeners() {
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                RetrievePwdActivity.this.finish();
            }
        });

        //发送验证码
        tvSendCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (etPhone.getText().toString().trim().equals("")) {
                        ToastUtils.show("请输入手机号");
                    } else {
                        sendCodePresenter.sendCode(etPhone.getText().toString().trim());
                        time.start();
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //确定
        tvSubmit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (etPhone.getText().toString().trim().equals("")) {
                        ToastUtils.show("请输入手机号");
                    } else if (etCode.getText().toString().trim().equals("")) {
                        ToastUtils.show("请输入验证码");
                    } else if (etPassword.getText().toString().trim().equals("")) {
                        ToastUtils.show("请输入密码");
                    } else if (!isSendCode) {
                        ToastUtils.show("请先发送验证码");
                    } else {
                        findPwdPresenter.findPwd(etPhone.getText().toString().trim(), etPassword.getText().toString().trim(), etCode.getText().toString().trim());
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    //发送验证码回调
    @Override
    public void showCode() {
        isSendCode = true;
    }


    //忘记密码回调
    @Override
    public void showResult() {
        ToastUtils.show("修改密码成功");
        this.finish();
    }

    @Override
    public void show401() {
        this.finish();
        Intent intent = new Intent(RetrievePwdActivity.this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(this).putString(Constant.TOKEN, "");
    }

    //倒计时
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvSendCode.setText(millisUntilFinished / 1000 + "s后重新获取");
            tvSendCode.setClickable(false);
        }

        @Override
        public void onFinish() {
            tvSendCode.setText("获取验证码");
            tvSendCode.setClickable(true);
        }
    }
}
